package com.qxp.simplerpc.retry;

import com.qxp.simplerpc.connect.RpcClient;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 启动重连监听
 */
@Component
public class ConnectionListener implements ChannelFutureListener {
    private static Logger logger = LoggerFactory.getLogger(ConnectionListener.class);
    @Autowired
    private RpcClient rpcClient;
    @Override
    public void operationComplete(ChannelFuture channelFuture)  {
        if (!channelFuture.isSuccess()) {
            final EventLoop loop = channelFuture.channel().eventLoop();
            loop.schedule(() -> {
                logger.error("simple rpc server can't been required...retry in 3 second..please wait..");
                rpcClient.run();
            }, 3L, TimeUnit.SECONDS);
        } else {
            logger.info("simple rpc server connect success");
        }
    }
}
